﻿@{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();

    var grid = Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gvEditing";
            settings.KeyFieldName = "IdInsumo";
            settings.CallbackRouteValues = new { Controller = "Insumo", Action = "EditInsumoPartial" };
            settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Insumo", Action = "EditInsumoAddNewPartial" };
            settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Insumo", Action = "EditInsumoUpdatePartial" };
            settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Insumo", Action = "EditInsumoDeletePartial" };
            settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
            
            
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsPopup.EditForm.Width = 600;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
            settings.SettingsPager.FirstPageButton.Visible = true;
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };
            
            
            settings.Settings.ShowFilterRow = true;
            settings.Settings.ShowFilterRowMenu = true;
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowClearFilterButton = true;
            
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            settings.CommandColumn.Visible = true;
            //settings.CommandColumn.ShowNewButtonInHeader = ViewBag.PermisoCreate;
            //settings.CommandColumn.ShowEditButton = ViewBag.PermisoEdit;
            //settings.CommandColumn.ShowDeleteButton = ViewBag.PermisoDelete;

            settings.CommandColumn.ShowNewButtonInHeader = true;
            settings.CommandColumn.ShowEditButton = true;
            settings.CommandColumn.ShowDeleteButton = true;

            //settings.Columns.Add("IdInsumo");
            settings.Columns.Add("Nombre");            
            settings.Columns.Add("Descripcion");
            settings.Columns.Add("StockNecesario");
            settings.Columns.Add(column =>
            {
                column.FieldName = "UnidadDeMedida.IdUnidadMedida";
                column.Caption = "Unidad";

                column.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                comboBoxProperties.DataSource = ViewBag.Unidad;
                comboBoxProperties.TextField = "Nombre";
                comboBoxProperties.ValueField = "IdUnidadMedida";
                comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
                comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;

                column.SetEditItemTemplateContent(container =>
                {
                    Html.DevExpress().ComboBox(editorSettings =>
                    {
                        editorSettings.Name = "IdUnidadMedida";
                        editorSettings.Properties.TextField = "Nombre";
                        editorSettings.Properties.ValueField = "IdUnidadMedida";
                        editorSettings.Properties.Width = 120;
                        editorSettings.Properties.ValueType = typeof(int);
                        editorSettings.Properties.ValidationSettings.EnableCustomValidation = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                        editorSettings.ShowModelErrors = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip;
                        editorSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

                    }).BindList(ViewBag.Unidad).Bind(DataBinder.Eval(container.DataItem, "UnidadDeMedida.IdUnidadMedida")).Render();
                });
            });

            settings.PreRender = (s, e) =>
            {
                var sender = (MVCxGridView)s;
                sender.FocusedRowIndex = 0;
            };

            settings.SettingsBehavior.AllowFocusedRow = true;

            
        });

    if (ViewData["EditError"] != null){
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}

@grid.Bind(Model).GetHtml()










